﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using BIExtAuth.BLL;
using BIExtAuth.Model;

namespace BIExtAuth.Web.Admin
{
    public partial class EditMember : System.Web.UI.Page
    {
        UserBLL userBLL = new UserBLL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.txtUserID.Text.Trim() == "")
            {
                msg.Text = "查无此用户！";
            }
            else
            {
                try
                {
                    BIUser user = this.userBLL.GetUserByUserID(this.txtUserID.Text.Trim());
                    if (user == null)
                    {
                        msg.Text = "查无此用户！";
                    }
                    else
                    {
                        this.txtUserID.Text = user.UserID;
                        this.txtFullName.Text = user.UserName;
                        this.txtDesc.Text = user.UserDesc;
                        this.txtEmail.Text = user.UserEmail;
                        this.ddlState.SelectedValue = user.UserStatus == true ? "1" : "0";
                    }
                }
                catch (Exception ex)
                {
                    msg.Text = "系统错误。查无此用户！\n" + ex.Message;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            BIUser adminUser = this.userBLL.GetUserByUserID(this.txtAdminUser.Text.Trim());
            if (adminUser.UserID == "biadmin" && adminUser.UserPwd == BIExtAuth.Common.SecurityEncrypt.DesEncrypt(this.txtAdminPwd.Text))
            {
                BIUser user = new BIUser();
                user.UserID = this.txtUserID.Text.Trim();
                user.UserName = this.txtFullName.Text.Trim();
                user.UserDesc = this.txtDesc.Text.Trim();
                user.UserEmail = this.txtEmail.Text.Trim();
                user.UserStatus = this.ddlState.SelectedValue == "1" ? true : false;

                try
                {
                    int ret = this.userBLL.UpdateUser(user);
                    if (ret > 0)
                    {
                        msg.Text = "编辑用户成功！";
                        this.txtUserID.Text = "";
                        this.txtFullName.Text = "";
                        this.txtDesc.Text = "";
                        this.txtEmail.Text = "";
                        this.ddlState.SelectedIndex = 0;
                        this.txtAdminUser.Text = "";
                        this.txtUserID.Focus();
                    }
                    else
                    {
                        msg.Text = "编辑用户失败！";
                    }
                }
                catch (Exception ex)
                {
                    msg.Text = "系统错误。编辑用户失败！\n" + ex.Message;
                }
            }
            else
            {
                msg.Text = "管理员账号或密码错误！";
            }
        }
    }
}